// nearbydetail.js

const db = wx.cloud.database();

Page({
  data: {
    user:'',
    nearbyDetail: {}, // 存储景点详情数据
    swiperImages: [], // 存储轮播图图片地址
    showBuyPanel: false,
    quantity: 1,
    goodsDetail: {} // 商品详情
  },
  onLoad: function(options) {
    // 获取传递过来的景点 id
    const nearbyId = options.id;
    // 获取景点详情数据
    this.getNearbyDetail(nearbyId);
    // 获取用户当前位置
  },
  getNearbyDetail: function(nearbyId) {
    // 使用云开发获取景点详情数据
    db.collection('zyzl_shopping').doc(nearbyId).get().then(res => {
      console.log(res);
      // 更新页面数据
      this.setData({
        // console.log(res),
        nearbyDetail: res.data,
        swiperImages: [res.data.img1, res.data.img2, res.data.img3]
      });
    }).catch(err => {
      console.error('获取景点详情数据失败：', err);
    });
  },
  onShow() {
    this.setData({
      user: wx.getStorageSync('user') || ''
    })
  },
  shop() {
    // 检查用户登录状态
    const user = wx.getStorageSync('user');
    if (!user) {
      // 用户未登录，弹窗提示登录
      wx.showModal({
        title: '提示',
        content: '请先登录',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/my/login/login'
            });
          }
        }
      });
      return;
    }

    // 设置商品详情并显示购买面板
    this.setData({
      goodsDetail: {
        img: this.data.nearbyDetail.img1,
        price: this.data.nearbyDetail.price,
        stock: 999, // 这里可以设置实际的库存数量
        shopname: this.data.nearbyDetail.shopname
      },
      showBuyPanel: true
    });
  },
  // 显示购买面板
  showBuyPanel() {
    this.setData({
      showBuyPanel: true
    });
  },
  // 关闭购买面板
  closeBuyPanel() {
    this.setData({
      showBuyPanel: false
    });
  },
  // 减少数量
  decreaseQuantity() {
    if (this.data.quantity > 1) {
      this.setData({
        quantity: this.data.quantity - 1
      });
    }
  },
  // 增加数量
  increaseQuantity() {
    if (this.data.quantity < this.data.goodsDetail.stock) {
      this.setData({
        quantity: this.data.quantity + 1
      });
    }
  },
  // 输入数量
  onQuantityInput(e) {
    let value = parseInt(e.detail.value);
    if (isNaN(value)) value = 1;
    if (value < 1) value = 1;
    if (value > this.data.goodsDetail.stock) value = this.data.goodsDetail.stock;
    
    this.setData({
      quantity: value
    });
  },
  // 确认购买
  confirmBuy() {
    const { quantity, goodsDetail } = this.data;
    const user = wx.getStorageSync('user');
    
    // 构建订单信息对象
    const orderInfo = {
      user: user,
      name: this.data.nearbyDetail.name,
      shopname: goodsDetail.shopname,
      price: goodsDetail.price,
      quantity: quantity,
      totalPrice: goodsDetail.price * quantity,
      timestamp: new Date()
    };

    // 将订单信息存入数据库
    wx.cloud.database().collection('zyzl_shopcart').add({
      data: orderInfo,
      success: res => {
        console.log('商品成功加入购物车：', res);
        wx.showToast({
          title: '成功加入购物车',
          icon: 'success',
          duration: 2000
        });
        this.closeBuyPanel();
      },
      fail: err => {
        console.error('商品加入购物车失败：', err);
        wx.showToast({
          title: '加入购物车失败',
          icon: 'none',
          duration: 2000
        });
      }
    });
  }
});
